<?php
/*
	申请退款页面
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['ischecklogin']=$this->model('member')->checklogin('','','','',true);
if($_RQ['op']=='list' || $_RQ['op']=='detail'){
	$return['params']['status']=0;
	if($_RQ['orderid']){
		$refund=$this->pdo_get('order_refund',array('orderid'=>$_RQ['orderid']),array('id','status'));
		$return['params']['status']=$refund['status'];
	}
	$return['status']=$this->model('order/refund')->status;
	set_html('title','售后');
	exi($return);
}
if($_RQ['op']=='getlist'){
	$params=array('mid'=>$_SESSION['uid']);
	$params['status']=intval($_RQ['status']);
	$psize=10;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('order_refund',$parameter);
	foreach($list as &$item){
		$item['orderstatus']=pdo_getcolumn('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['orderid']),'status');
		$item['orderispay']=pdo_getcolumn('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['orderid']),'ispay');
		$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		$item['typetitle']=$this->refund_types[$item['type']];
		$params=array('orderid'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']);
		if($item['ordergoodsid']){
			$params['id']=$item['ordergoodsid'];
		}
		$ordergoods=pdo_getall('jy_weishop_order_goods',$params,array('optionid','total','goodsid','istaocan','id'));
		foreach($ordergoods as &$gv){
				$gv['title']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['goodsid']),'title');
				//商品规格
				if($gv['optionid']){
					$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
					if($gv['istaocan']){
						$gv['selespename']=$options['title'];
					}else{
						$specs=$options['specs'];
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$gv['selespename']=implode("，",$specs);
					}
				}
		}
		$item['goods']=$ordergoods;
	}
	if(!$list){
		exi('none');
	}
	exi($list);
}

if($_RQ['op']=='apply'){
	if($_RQ['postdata']){	
		$post=get_postdata();
		if($post['needchecked'] && !$post['checked']){
			exi('请先阅读《售后协议》！','error');
		}
		if($post['price']>$post['maxprice']){
			exi('申请失败，金额不能大于最大金额！','error');
		}
		if(!$post['isup']){
			$this->model('order/refund')->check_order_rerefund(intval($post['orderid']),true,$post['goodsid'],$post['ordergoodsid']);
		}
		if($post['type']==4){
			$post['price']=$post['dispatchprice'];
		}
		if($post['type']==1 || $post['type']==2){
			$post['price']=0;
		}
		$params=array(
			'type'=>intval($post['type']),
			'orderid'=>intval($post['orderid']),
			'ordergoodsid'=>intval($post['ordergoodsid']),
			'images'=>$post['images']?ijson_encode($post['images']):'',
			'price'=>$post['price'],
			'content'=>$post['content'],
			'videos'=>$post['videos'],
			'msgtypes'=>$post['msgtypes'],
			'goodsnum'=>$post['goodsnum'],
		);
		$this->model('order/refund')->refund_create($params);
		exi('申请成功！请耐心等待管理员审核！','','list');
	}
	$return['refund_types']=$this->refund_types;
	$orderid=intval($_RQ['orderid']);
	if(!$orderid){
		exi('请先选择订单！','error');
	}
	$ordergoodsid=intval($_RQ['ordergoodsid']);
	$return['order']=$this->pdo_get('order',array('id'=>$orderid),array('price','dispatchprice','status','ordersn'));
	$return['order']['statustitle']=$this->order_status[$return['order']['status']];
	$params=array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']);
	$allcount=pdo_count('jy_weishop_order_goods',$params,'id');
	if($ordergoodsid){
		$params['id']=$ordergoodsid;
		$this->model('order')->setfee_desc($orderid,true);
	}else{
		$params['status <']=5;
	}
	//剩余未申请的商品
	$count=pdo_count('jy_weishop_order_goods',array('status <'=>5,'orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'id');
	$ordergoods=pdo_getall('jy_weishop_order_goods',$params,array('goodsid','istaocan','optionid','price','total','total_price','goodssn','productsn'));
	foreach($ordergoods as &$gv){
		$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
		$gv['title']=$goodsinfo['title'];
		$gv['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
		//商品规格
		if($gv['optionid']){
			$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
			if($gv['istaocan']){
				$gv['selespename']=$options['title'];
			}else{
				$specs=$options['specs'];
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$gv['selespename']=implode("，",$specs);
			}
		}
		$nowgoodsprice=$gv['total_price'];
	}
	$return['order']['goods']=$ordergoods;
	if($return['order']['status']>1){
		$return['order']['endprice']=$return['order']['price']-$return['order']['dispatchprice'];
	}else{
		$return['order']['endprice']=$return['order']['price'];
	}
	if($ordergoodsid){
		$return['ordergoodsid']=$ordergoodsid;
	}
	$params=array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']);
	$params['ordergoodsid']=$ordergoodsid;
	$item=$this->pdo_get('order_refund',$params);
	if(!$item){
		if(!$ordergoodsid){
			if($allcount==$count){
				$return['order']['endprice']=$return['order']['price'];
			}else{
				$isfee=pdo_getsum('jy_weishop_order_refund',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'price');
				$return['order']['endprice']=$return['order']['price']-$isfee;
			}
		}elseif($ordergoodsid){
			//最后一个商品时
			if($allcount>$count && $count==1){
				$isfee=pdo_getsum('jy_weishop_order_refund',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'price');
				$return['order']['endprice']=$return['order']['price']-$isfee;
			}elseif($allcount==$count){
				$return['order']['endprice']=$return['order']['price'];
			}else{
				$return['order']['endprice']=$nowgoodsprice;
			}
		}
		$return['order']['endprice']=$return['order']['endprice']<0?0:$return['order']['endprice'];
		$return['order']['endprice']=intval($return['order']['endprice']*100)/100;
		$item=array(
			'type'=>'0',
			'isup'=>intval($_RQ['isup']),
			'orderid'=>$orderid,
			'images'=>array(),
		);
		$item['price']=$return['order']['endprice'];
		$item['maxprice']=$return['order']['endprice'];
		$item['ordergoodsid']=$return['ordergoodsid'];
		//是否售后？
		$return['can_refund']=$this->model('order/refund')->check_order_rerefund($orderid,'',$ordergoods[0]['goodsid'],$ordergoodsid);
		if($return['can_refund']==2){
			$item['type']=3;
		}
	}else{
		$item['isup']=1;
		$item['images']=$item['images']?json_decode($item['images'],true):array();
		$item['maxprice']=$item['price'];
	}
	$item['goodsid']=$ordergoods[0]['goodsid'];
	$item['checked']=true;
	$return['item']=$item;
	$refundset=$this->plugin_setting('refund');
	$return['msgtypes']=$refundset['msgtypes']?:'质量问题|缺货|损伤|品种出错';
	$return['msgtypes']=str_array($return['msgtypes'],'|');
	$return['demoimgs']=$refundset['demoimgs'];
	foreach($return['demoimgs'] as &$img){
		$img=tomedia($img);
	}
	if(!$refundset['xieyishow']){
		$return['show_xieyi']=1;
	}
	$return['refundset']=$refundset;
	exi($return);
}
/*
	退款商品发货
*/
if($_RQ['op']=='send'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$params=array(
			'com'=>$post['com'],
			'sn'=>$post['sn'],
		);
		$this->model('order/refund')->refund_send(intval($post['id']),$params);
		exi('退货商品发货成功！','','goback');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('order_refund',array('id'=>$id),array('id','status','express'));
	$item['express']=json_decode($item['express'],true);
	$item['com']=$item['express']['com'];
	$item['sn']=$item['express']['sn'];
	if($item['status']!=1 && $item['status']!=2){
		exi('不符合发货条件！','error');
	}
	$return['item']=$item;
	$return['express_types']=$this->model('model')->express_types();
    $return['refundset']=$this->plugin_setting('refund');
    $return['refundset']['addressinfo']=model('core/member')->get_address_str($return['refundset']);
	exi($return);
}
if($_RQ['op']=='cancel'){
	$post=get_postdata();	
	$id=intval($post['id']);
	if($id){
		$orderid=pdo_getcolumn('jy_weishop_order_refund',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'orderid');
		$oldstatus=pdo_getcolumn('jy_weishop_order',array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']),'oldstatus');
		$this->pdo_update('order',array('status'=>$oldstatus,'oldstatus'=>0),array('id'=>$orderid));
		pdo_delete('jy_weishop_order_refund',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
		exi('取消成功！','error','list');
	}else{		
		exi('取消失败！','error');
	}

}